﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Scenario.WorkflowEngine.DataModel.WorkflowProcesses;
using Scenario.WorkflowEngine.DataModel.WorkflowParticipantsData;
using Scenario.Tools.UI.Controls.ListControls.ListItem;
using Scenario.WorkflowEngine;

namespace Scenario.Tools.UI.Controls.ListControls
{
    public partial class ActivityList : UserControl
    {
        public ActivityList()
        {
            InitializeComponent();
            this.MakeGrid();
        }

        private void MakeGrid()
        {
            ColumnHeader ch = new ColumnHeader();
            ch.Text = "ProcessId";
            ch.TextAlign = HorizontalAlignment.Right;
            ch.Width = 100;
            lstActivities.Columns.Add(ch);

            ch = new ColumnHeader();
            ch.Text = "Name";
            ch.TextAlign = HorizontalAlignment.Left;
            ch.Width = 100;
            lstActivities.Columns.Add(ch);

            
            ch = new ColumnHeader();
            ch.Text = "Data";
            ch.TextAlign = HorizontalAlignment.Left;
            ch.Width = 100;
            lstActivities.Columns.Add(ch);


            ch = new ColumnHeader();
            ch.Text = "Owner";
            ch.TextAlign = HorizontalAlignment.Left;
            ch.Width = 100;
            lstActivities.Columns.Add(ch);

            
            ch = new ColumnHeader();
            ch.Text = "Status";
            ch.TextAlign = HorizontalAlignment.Left;
            ch.Width = 100;
            lstActivities.Columns.Add(ch);

            ch = new ColumnHeader();
            ch.Text = "Result";
            ch.TextAlign = HorizontalAlignment.Left;
            ch.Width = 100;
            lstActivities.Columns.Add(ch);

            ch = new ColumnHeader();
            ch.Text = "Type";
            ch.TextAlign = HorizontalAlignment.Left;
            ch.Width = 100;
            lstActivities.Columns.Add(ch);
        }

        private void ActivityList_Load(object sender, EventArgs e)
        {
           
            this.cmbType.Items.Add(new ActivityTypeComboItem(ActivityType.UserLocked,  "Utente - Personali"));
            this.cmbType.Items.Add(new ActivityTypeComboItem(ActivityType.Available,   "Utente - Disponibili"));
            this.cmbType.Items.Add(new ActivityTypeComboItem(ActivityType.UserVisible, "Utente - Tutte"));
            this.cmbType.Items.Add(new ActivityTypeComboItem(ActivityType.UserNotified,"Utente - Notifiche"));
            this.cmbType.Items.Add(new ActivityTypeComboItem(ActivityType.All, "Tutte"));
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            this.lstActivities.Items.Clear();

            XMLActivities xmlActivities = null;
            ActivityTypeComboItem activityType = (ActivityTypeComboItem)this.cmbType.SelectedItem;

            switch (activityType.Type)
            {
                case ActivityType.UserLocked:
                    xmlActivities = Engine.ActivityHelper.GetActivities(
                        Scenario.WAPI.ActivityHelper.ActivityList.LockedByUser,
                        string.Empty,
                        this.cmbUsers.SelectedItem.ToString());
                    break;
                case ActivityType.Available:
                    xmlActivities = Engine.ActivityHelper.GetActivities(
                        Scenario.WAPI.ActivityHelper.ActivityList.AvailableByUser,
                        string.Empty,
                        this.cmbUsers.SelectedItem.ToString()); 
                    break;
                case ActivityType.UserNotified:
                        xmlActivities = Engine.ActivityHelper.GetActivities(
                            Scenario.WAPI.ActivityHelper.ActivityList.Notifications,
                            string.Empty,
                            this.cmbUsers.SelectedItem.ToString()); 
                    break;
                case ActivityType.UserVisible:
                    xmlActivities = Engine.ActivityHelper.GetActivities(
                        Scenario.WAPI.ActivityHelper.ActivityList.VisibileByUser,
                        string.Empty,
                        this.cmbUsers.SelectedItem.ToString());
                    break;
                case ActivityType.All:
                    xmlActivities = Engine.ActivityHelper.GetActivities(
                        Scenario.WAPI.ActivityHelper.ActivityList.All,
                        string.Empty,
                        this.cmbUsers.SelectedItem.ToString());
                    break;
            }

            foreach (XMLActivity activity in  xmlActivities)
            {
                ListViewItem item = new ListViewItem();
                item.Tag = activity;
                item.Text = activity.ProcessId.ToString();
                item.SubItems.Add(activity.Name);
                item.SubItems.Add(activity.Type);
                item.SubItems.Add(activity.XMLLog.Owner);
                item.SubItems.Add(activity.XMLLog.Date);
                item.SubItems.Add(activity.XMLLog.Result);
                item.SubItems.Add(activity.XMLLog.Status);
                this.lstActivities.Items.Add(item);
            }
        }

        private void mnuDetails_Click(object sender, EventArgs e)
        {
            MessageBox.Show("TODO");
        }

        private void mnuRestartProcess_Click(object sender, EventArgs e)
        {
            if (this.lstActivities.SelectedItems.Count == 0)
            {
                MessageBox.Show("Select an activity");
                return;
            }
            else if (this.lstActivities.SelectedItems.Count > 1)
            {
                MessageBox.Show("Select only one activity");
                return;
            }

            XMLActivity activity = (XMLActivity)this.lstActivities.SelectedItems[0].Tag;
            Scenario.Forms.Workflow.ProcessInstanceManager instanceManager =
                    new Scenario.Forms.Workflow.ProcessInstanceManager();
            instanceManager.LoadProcess(activity.ProcessId);

            instanceManager.ShowDialog();
        }
    }
}
